package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wb.domain.WbFireServiceUnit;
import com.ruoyi.wb.domain.WbFireServiceUser;
import com.ruoyi.wb.dto.WbFireServiceUnitDto;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbFireServiceUnitMapper;
import com.ruoyi.wb.service.IWbFireServiceUnitService;
import com.ruoyi.wb.service.IWbFireServiceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;


/**
 * 消防单位Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class WbFireServiceUnitServiceImpl implements IWbFireServiceUnitService {
    @Autowired
    private WbFireServiceUnitMapper wbFireServiceUnitMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IWbFireServiceUserService wbFireServiceUserService;


    /**
     * 查询消防单位
     *
     * @param id 消防单位主键
     * @return 消防单位
     */
    @Override
    public WbFireServiceUnit selectWbFireServiceUnitById(Long id) {
        return wbFireServiceUnitMapper.selectWbFireServiceUnitById(id);
    }

    /**
     * 查询消防单位列表
     *
     * @param wbFireServiceUnit 消防单位
     * @return 消防单位
     */
    @Override
    public List<WbFireServiceUnit> selectWbFireServiceUnitList(WbFireServiceUnit wbFireServiceUnit) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        WbFireServiceUnit wbFireServiceUnit1 = wbFireServiceUnitMapper.getFireServiceUnitByUserId(loginUser.getUserId());
        if (!ObjectUtils.isEmpty(wbFireServiceUnit1)){
            wbFireServiceUnit.setId(wbFireServiceUnit1.getId());
        }

        return wbFireServiceUnitMapper.selectWbFireServiceUnitList(wbFireServiceUnit);
    }

    /**
     * 新增消防单位
     *
     * @param wbFireServiceUnit 消防单位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWbFireServiceUnit(WbFireServiceUnitDto wbFireServiceUnitDto) {
        //新增消防单位
        LoginUser loginUser = SecurityUtils.getLoginUser();
        wbFireServiceUnitDto.setCreateTime(DateUtils.getNowDate());
        wbFireServiceUnitDto.setUpdateTime(DateUtils.getNowDate());
        wbFireServiceUnitDto.setCreateBy(loginUser.getUserId().toString());
        wbFireServiceUnitDto.setUpdateBy(loginUser.getUserId().toString());
        WbFireServiceUnit wbFireServiceUnit = BeanUtil.copyProperties(wbFireServiceUnitDto, WbFireServiceUnit.class);
        int i2 = wbFireServiceUnitMapper.insertWbFireServiceUnit(wbFireServiceUnit);
        if (i2 == 0) {
            throw new RuntimeException("新增消防单位失败");
        }
        //新增消防单位管理员  和  用户角色关联表
        SysUser sysUser = wbFireServiceUnitDto.getSysUser();
        //校验用户名
        boolean b = sysUserService.checkUserNameUnique(sysUser);
        if (!b) {
            throw new RuntimeException("用户名已存在");
        }
        //校验手机号
        boolean p = sysUserService.checkPhoneUnique(sysUser);
        if (!p) {
            throw new RuntimeException("手机号已存在");
        }

        if (StringUtils.isEmpty(sysUser.getPassword())||sysUser.getPassword().length()<5){
            throw new RuntimeException("密码长度必须大于5");
        }
        sysUser.setNickName(wbFireServiceUnitDto.getFunctionary());
        sysUser.setPassword(SecurityUtils.encryptPassword(sysUser.getPassword()));
        sysUser.setPhonenumber(wbFireServiceUnitDto.getPhone());
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUser.setCreateBy(loginUser.getUserId().toString());
        sysUser.setUpdateBy(loginUser.getUserId().toString());
        sysUser.setRoleId(Long.valueOf(RoleNameEnums.FIRE_UNIT_MANAGER.getType()));//给个消防单位管理员的角色

        int i = sysUserService.insertUser(sysUser);
        if (i == 0) {
            throw new RuntimeException("新增用户失败");
        }

        //新增 消防单位和用户关联表
        WbFireServiceUser wbFireServiceUser = new WbFireServiceUser();
        wbFireServiceUser.setUserId(sysUser.getUserId());
        wbFireServiceUser.setFireServiceUnitId(wbFireServiceUnit.getId());

        int i1 = wbFireServiceUserService.insertWbFireServiceUser(wbFireServiceUser);
        if (i1 == 0) {
            throw new RuntimeException("新增消防用户失败");
        }

        return i2 ;
    }

    /**
     * 修改消防单位
     *
     * @param wbFireServiceUnit 消防单位
     * @return 结果
     */
    @Override
    public int updateWbFireServiceUnit(WbFireServiceUnit wbFireServiceUnit) {
        wbFireServiceUnit.setUpdateTime(DateUtils.getNowDate());
        return wbFireServiceUnitMapper.updateWbFireServiceUnit(wbFireServiceUnit);
    }

    /**
     * 批量删除消防单位
     *
     * @param ids 需要删除的消防单位主键
     * @return 结果
     */
    @Override
    public int deleteWbFireServiceUnitByIds(Long[] ids) {
        return wbFireServiceUnitMapper.deleteWbFireServiceUnitByIds(ids);
    }

    /**
     * 删除消防单位信息
     *
     * @param id 消防单位主键
     * @return 结果
     */
    @Override
    public int deleteWbFireServiceUnitById(Long id) {
        return wbFireServiceUnitMapper.deleteWbFireServiceUnitById(id);
    }

    @Override
    public WbFireServiceUnit getFireServiceUnitByUserId(Long userId) {

        return wbFireServiceUnitMapper.getFireServiceUnitByUserId(userId);
    }
}
